package com.hjq.demo.ui.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.RecyclerView
import com.bndg.browser.R
import com.hjq.base.BaseAdapter
import com.hjq.base.BaseDialog
import com.hjq.base.BottomSheetDialog
import com.hjq.base.action.AnimAction
import com.bndg.browser.app.AppAdapter

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject-Kotlin
 *    time   : 2019/07/27
 *    desc   : 相册专辑选取对话框
 */
class BottomMenuDialog {

    class Builder(context: Context) : BaseDialog.Builder<Builder>(context),
        BaseAdapter.OnItemClickListener {

        private var listener: OnListener? = null
        private val recyclerView: RecyclerView? by lazy { findViewById(R.id.rv_menu_list) }
        private val adapter: MenuAdapter

        init {
            setContentView(R.layout.menu_dialog)
            setAnimStyle(AnimAction.ANIM_EMPTY)
            adapter = MenuAdapter(context)
            adapter.setOnItemClickListener(this)
            recyclerView?.adapter = adapter
            recyclerView?.setItemAnimator(null);
            val data = mutableListOf<MenuInfo>()
            data.add(MenuInfo(R.drawable.baseline_download_24, "下载管理"))
            data.add(MenuInfo(R.drawable.baseline_collections_bookmark_24, "书签"))
            data.add(MenuInfo(R.drawable.baseline_history_24, "历史"))
            data.add(MenuInfo(R.drawable.baseline_bookmark_add_24, "添加"))
            setData(data)
        }

        fun setData(data: MutableList<MenuInfo>): Builder = apply {
            adapter.setData(data)
            // 滚动到选中的位置
        }

        fun setListener(listener: OnListener?): Builder = apply {
            this.listener = listener
        }

        override fun onItemClick(recyclerView: RecyclerView?, itemView: View?, position: Int) {
            // 延迟消失
            postDelayed({
                listener?.onSelected(getDialog(), position, adapter.getItem(position))
                dismiss()
            }, 300)
        }

        override fun createDialog(context: Context, themeId: Int): BaseDialog {
            val dialog = BottomSheetDialog(context, themeId)
            dialog.getBottomSheetBehavior().peekHeight =
                getResources().displayMetrics.heightPixels / 2
            return dialog
        }
    }

    class MenuAdapter constructor(context: Context) : AppAdapter<MenuInfo>(context) {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder()
        }

        inner class ViewHolder : AppViewHolder(R.layout.menu_item) {

            private val iconView: ImageView? by lazy { findViewById(R.id.iv_icon) }
            private val nameView: TextView? by lazy { findViewById(R.id.tv_name) }

            override fun onBindView(position: Int) {
                getItem(position).apply {
                    iconView?.let {
                        it.setImageResource(getIcon())
                    }
                    nameView?.text = getName()
                }
            }
        }
    }

    /**
     * 专辑信息类
     */
    class MenuInfo(

        /** 封面 */
        @DrawableRes
        private val icon: Int,
        /** 名称 */
        private var name: String,
    ) {
        fun getIcon(): Int {
            return icon
        }

        fun getName(): String {
            return name
        }

        fun isHistory(): Boolean {
            return name == "历史"
        }
    }

    interface OnListener {

        /**
         * 选择条目时回调
         */
        fun onSelected(dialog: BaseDialog?, position: Int, bean: MenuInfo)
    }
}